package com.example.hfweather.ui.home

import android.os.Handler
import android.os.Looper
import com.example.hfweather.base.BaseInteractor
import com.example.hfweather.util.WeatherUtils
import com.qweather.sdk.QWeather
import com.qweather.sdk.basic.Lang
import com.qweather.sdk.parameter.weather.WeatherParameter
import com.qweather.sdk.response.weather.WeatherNowResponse
import com.qweather.sdk.Callback
import com.qweather.sdk.basic.Range
import com.qweather.sdk.parameter.air.AirV1Parameter
import com.qweather.sdk.parameter.geo.GeoCityTopParameter
import com.qweather.sdk.response.air.v1.AirV1CurrentResponse
import com.qweather.sdk.response.air.v1.AirV1HourlyResponse
import com.qweather.sdk.response.error.ErrorResponse
import com.qweather.sdk.response.geo.GeoCityTopResponse
import com.qweather.sdk.response.weather.WeatherHourlyResponse

class HomeInteractor: BaseInteractor<HomeView>() {
    //获取当前天气数据接口
    interface GetMainWeatherData {
        fun getWeatherDataSuccess(response: WeatherNowResponse)
        fun getWeatherDataFailed(message: String)
    }

    /**
     * 每小时天气数据获取结果回调接口
     * 提供成功与失败两种回调方法
     */
    interface GetHourlyWeatherData {
        /**
         * 每小时天气数据获取成功时调用
         * @param response 包含每小时天气信息的响应对象
         */
        fun getHourlyWeatherDataSuccess(response: WeatherHourlyResponse)

        /**
         * 每小时天气数据获取失败时调用
         * @param errorResponse 错误信息描述
         */
        fun getHourlyWeatherDataFailure(errorResponse: String)

    }


    /**
     * 获取每小时空气质量 数据获取结果回调接口
     * 提供成功与失败两种回调方法
     */
    interface GetHourlyAirQualityData {
        /**
         * 每小时空气质量数据获取成功时调用
         * @param response 每小时空气质量数据
         */
        fun getHourlyAirQualityDataSuccess(response: AirV1HourlyResponse)
        /**
         * 每小时空气质量数据获取失败时调用
         * @param errorResponse 错误信息
         */
        fun getHourlyAirQualityDataFailure(errorResponse: String)
    }

    /**
     * 获取当前实时空气质量数据获取结果回调接口
     *  提供成功与失败两种回调方法
     */
    interface GetCurrentAirQualityData {
        /**
         * 获取当前实时空气质量数据获取成功时调用
         * @param response 实时空气质量数据
         */
        fun getCurrentAirQualityDataSuccess(response: AirV1CurrentResponse)
        /**
         * 获取当前实时空气质量数据获取失败时调用
         * @param errorResponse 错误信息
         */
        fun getCurrentAirQualityDataFailure(errorResponse: String)
    }

    /**
     * 获取城市列表数据获取结果回调接口
     */
    interface GetTopCityListData {
        /**
         * 获取城市列表数据获取成功时调用
         */
        fun getTopCityDataSuccess(response: GeoCityTopResponse)
        /**
         * 获取城市列表数据获取失败时调用
         */
        fun getTopCityDataFailure(errorResponse: String)
    }

    //1.获取当前城市的天气数据
    fun getCityCurrentWeather(latitude: String, longitude: String, callback: GetMainWeatherData) {
        //1.1准备参数
        val tempParam = WeatherUtils.formatLatLng(longitude)+","+WeatherUtils.formatLatLng(latitude)
        val parameter= WeatherParameter(tempParam).lang(Lang.ZH_HANS).unit(com.qweather.sdk.basic.Unit.METRIC)
        //1.2发送请求
        QWeather.instance.weatherNow( parameter, object : Callback<WeatherNowResponse>{
            override fun onSuccess(res: WeatherNowResponse?) {
                //请求天气数据成功，切换到主线程调用接口
                Handler(Looper.getMainLooper()).post {
                    //1.3处理数据
                    if (res != null) {
                        //1.4返回数据
                        callback.getWeatherDataSuccess(res)
                    }
                }
            }

            override fun onFailure(error: ErrorResponse?) {
                Handler(Looper.getMainLooper()).post {
                    //1.5处理数据
                    callback.getWeatherDataFailed(error.toString())
                }
            }

            override fun onException(e: Throwable?) {
                Handler(Looper.getMainLooper()).post {
                    //1.6处理数据
                    callback.getWeatherDataFailed(e.toString())
                }
                e?.printStackTrace()
            }
        })
    }

    /**
     * 2. 获取当前城市24小时天气
     */
    fun getCityHourlyWeather(latitude: String, longitude: String, callback: GetHourlyWeatherData) {
        val tempParam =
            WeatherUtils.formatLatLng(longitude) + "," + WeatherUtils.formatLatLng(latitude)
        val parameter = WeatherParameter(tempParam)
        QWeather.instance.weather24h(parameter, object : Callback<WeatherHourlyResponse> {
            override fun onSuccess(response: WeatherHourlyResponse) {
                Handler(Looper.getMainLooper()).post {
                    callback.getHourlyWeatherDataSuccess(response)
                }
            }
            override fun onFailure(errorResponse: ErrorResponse) {
                Handler(Looper.getMainLooper()).post {
                    callback.getHourlyWeatherDataFailure(errorResponse.toString())
                }
            }
            override fun onException(e: Throwable) {
                Handler(Looper.getMainLooper()).post {
                    callback.getHourlyWeatherDataFailure(e.toString())
                }
                e.printStackTrace()
            }

        })
    }


    //3. 获取空气质量数据
    fun getCityAirQuality(latitude: String, longitude: String, callback: GetHourlyAirQualityData) {
        val lat = latitude.toDoubleOrNull()
        val lon = longitude.toDoubleOrNull()

        if (lat == null || lon == null) {
            Handler(Looper.getMainLooper()).post {
                callback.getHourlyAirQualityDataFailure("Invalid latitude or longitude format")
            }
            return
        }

        val airPar = AirV1Parameter(lat, lon)


        QWeather.instance.airHourly( airPar, object : Callback<AirV1HourlyResponse> {
            override fun onSuccess(p0: AirV1HourlyResponse?) {
                Handler(Looper.getMainLooper()).post {
                    callback.getHourlyAirQualityDataSuccess(p0!!)
                }
            }

            override fun onFailure(p0: ErrorResponse?) {
                Handler(Looper.getMainLooper()).post {
                    callback.getHourlyAirQualityDataFailure(p0.toString())
                }
            }

            override fun onException(p0: Throwable?) {
                Handler(Looper.getMainLooper()).post {
                    callback.getHourlyAirQualityDataFailure(p0.toString())
                }
                p0!!.printStackTrace()
            }
        })

    }
    //4.获取当前城市空气质量数据
    fun getCityCurrentAirQuality(latitude: String, longitude: String, callback: GetCurrentAirQualityData) {
        val lat = latitude.toDoubleOrNull()
        val lon = longitude.toDoubleOrNull()

        if (lat == null || lon == null) {
            Handler(Looper.getMainLooper()).post {
                callback.getCurrentAirQualityDataFailure("Invalid latitude or longitude format")
            }
            return
        }

        val airPar = AirV1Parameter(lat, lon)
        QWeather.instance.airCurrent(airPar, object : Callback<AirV1CurrentResponse> {
            override fun onSuccess(p0: AirV1CurrentResponse?) {
                Handler(Looper.getMainLooper()).post {
                    callback.getCurrentAirQualityDataSuccess(p0!!)
                }
            }

            override fun onFailure(p0: ErrorResponse?) {
                Handler(Looper.getMainLooper()).post {
                    callback.getCurrentAirQualityDataFailure(p0!!.toString())
                }
            }

            override fun onException(p0: Throwable?) {
                Handler(Looper.getMainLooper()).post {
                    callback.getCurrentAirQualityDataFailure(p0!!.message!!)
                }
                p0!!.printStackTrace()
            }

        })
    }

    // 获取城市列表
    fun getCityList(callback: GetTopCityListData) {
        val param = GeoCityTopParameter()
        param.number(20).range(Range.CN).lang(Lang.ZH_HANS)
        QWeather.instance.geoCityTop(param, object : Callback<GeoCityTopResponse> {
            override fun onSuccess(response: GeoCityTopResponse) {
                if (response.code == "200") {
                    Handler(Looper.getMainLooper()).post {
                        callback.getTopCityDataSuccess(response)
                    }
                } else {
                    Handler(Looper.getMainLooper()).post {
                        callback.getTopCityDataFailure(response.code)
                    }
                }
            }

            override fun onFailure(error: ErrorResponse?) {
                Handler(Looper.getMainLooper()).post {
                    callback.getTopCityDataFailure(error.toString())
                }
            }

            override fun onException(e: Throwable?) {
                Handler(Looper.getMainLooper()).post {
                    callback.getTopCityDataFailure(e.toString())
                    e?.printStackTrace()
                }
            }
        })

    }
}